#!/bin/bash
# Perfectbuntu by Robbie Ferguson - www.Category5.TV
# To install this file:  download it by saving it to your desktop, or wherever.
# Then, right-click the file, click Properties, and choose the Permissions tab.
# Check off "Allow Executing File as Program".
# Now, just run the file in terminal.
 
# For example:
# cd ~/Desktop
# ./perfectbuntu

# 6.0: Implement Ubuntu 11.04.

# 5.0: Completely redesigned the Q&A system, log file support, GUI
# removed; firefox, moovida

# 5.1: Added support for KDE (auto install gksudo)

# 5.2: Added support for Linux Mint Isadora

# 5.3: Added support for Ubuntu Maverick

# 5.4: Added detection of Linux version and passing to perfectbuntu6.current

# to add: Pogoplug Drive

WIDTH=350
VER="6.0"
LOGFILE="/tmp/perfectbuntu.log"

echo "Perfectbuntu $VER"
echo "Perfectbuntu $VER Log" > $LOGFILE
date "+%A %B %d, %Y - %r" >> $LOGFILE
echo "--------------------------------------------------" >> $LOGFILE

GKMSG="Perfectbuntu requires super user access in order to work its magic.  Please enter your super user password."

OSNAME=$(lsb_release -is)
# Detect KDE or Gnome as running window manager (thanks to odiseo77)
if [ "`ps -e | grep gdm`" ]; then
  ENVIRONMENT="gnome"
elif [ "`ps -e | grep kdm`" ]; then
  ENVIRONMENT="kde"
  echo -n "Checking for gksudo... " >> $LOGFILE
  if [ -e "/usr/bin/gksudo" ]; then
	echo "gksudo is already installed.  Proceeding with Perfectbuntu." >> $LOGFILE
  else
	# Install gksudo (which is required to run Perfectbuntu 5.0+ on KDE)
	echo -n "gksudo was not found, trying to install gksudo." >> $LOGFILE
	kdesudo --comment "$GKMSG" "apt-get --force-yes -y -f -m install gksu"
	if [ -e "/usr/bin/gksudo" ]; then
		echo "gksudo was installed successfully.  Proceeding with Perfectbuntu." >> $LOGFILE
	else
		echo "Installation of gksudo failed.  Exiting."
		exit 1
	fi
  fi
fi

echo -n "Checking for Zenity... " >> $LOGFILE
if [ -e "/usr/bin/zenity" ]; then
	echo "Zenity `zenity --version` is already installed.  Proceeding with Perfectbuntu." >> $LOGFILE
else
	# Install Zenity (which is required by Perfectbuntu 5.0+)
	echo -n "Zenity was not found, trying to install Zenity." >> $LOGFILE
	gksudo --message "Perfectbuntu requires super user access in order to work its magic.  Please enter your super user password." "apt-get --force-yes -y -f -m install zenity"
	if [ -e "/usr/bin/zenity" ]; then
		echo "Zenity `zenity --version` was installed successfully.  Proceeding with Perfectbuntu." >> $LOGFILE
	else
		echo "Installation of Zenity failed.  Exiting."
		exit 1
	fi
fi

(
echo "0" ; # Start Progress Indicator
echo "# Confirming super user access ..." ;
echo -n "Checking for super user access... " >> $LOGFILE

# Now for the code.  Don't worry about any of this stuff.  It's just nonsense.  ;)
gksudo --message "$GKMSG" "echo Super user access confirmed."

echo "1" ; # Increase Progress Indicator

gksudo touch /tmp/perfectbuntu.su; # This creates a temporary file, but only if user is super user.  We'll use it for authenticating.
if [ -e "/tmp/perfectbuntu.su" ]; then
	echo "# Super user access granted ..." ;
	gksudo rm /tmp/perfectbuntu.su
	echo "Super user access was granted.  Continuing." >> $LOGFILE
else
	echo "# Super user access denied ..." ;
	echo "Super user access was denied.  Exiting." >> $LOGFILE
	zenity --error --text="Access denied\r\n\r\nThe password you provided was not accepted by your system.  Please re-run Perfectbuntu and enter your super user password correctly.";
	exit 1
fi
sleep 1;

echo "2"; # Increase Progress Indicator
echo "# Detecting operating system version ...";

OUTPUT="<b>PERFECTBUNTU $VER</b>\r\nBy Robbie Ferguson (www.Category5.TV)\r\n\r\nWith additional contributions by:\r\n - Berry van der Linden (www.madberry.org)\r\n - Gord Campbell\r\n\r\n"

# Set the OS Version
OSVER=$(lsb_release -cs)
NAME=$(lsb_release -rs)

# Set allowed versions
if [ $OSVER = 'natty' ] || [ $OSVER = 'lucid' ] || [ $OSVER = 'isadora' ] || [ $OSVER = 'maverick' ]; then
  ALLOW="Yes"
else
  echo "# Incompatible version of $OSNAME.  Press OK to exit."
  echo "100"
fi

# set default bit to 32
BIT="32"
 
if [[ `uname -m` == 'amd64' || `uname -m` == 'x86_64' ]]; then
   BIT="64"
fi
 
OUTPUT=$OUTPUT"Detected version: $OSNAME $NAME, $BIT-bit\r\n"
OUTPUT=$OUTPUT"Kernel version: `uname -r`\r\n"

if [[ $ALLOW ]]; then
   OUTPUT=$OUTPUT"Supported Version: Yes\r\n\r\n"
   echo "Running under supported OS: $OSNAME $NAME, $BIT-bit / `uname -o` `uname -r`." >> $LOGFILE
else
   OUTPUT=$OUTPUT"Supported Version: No\r\n\r\nPress OK to exit."
   echo "Unsupported operating system.  Exiting." >> $LOGFILE
   zenity --title="Perfectbuntu $VER" --width $WIDTH --info --text="$OUTPUT"
   exit 1
fi

OUTPUT=$OUTPUT"Press OK to continue."
echo "3"; # Increase Progress Indicator
echo "# Waiting for user input ...";
zenity --title="Perfectbuntu $VER" --width $WIDTH --info --text="$OUTPUT"


echo "4"; # Increase Progress Indicator
echo "# Showing disclaimer ...";

zenity --warning --text="<b>Perfectbuntu is designed to be a helpful script, but can potentially break your system.</b>  By running this script, you acknowledge and accept that this could happen, and you take full responsibility for <u>all consequences</u> of running this script."


# Running a supported version of *buntu.  Move on.

echo "5"; # Increase Progress Indicator
echo "# Downloading the application list ...";

# Download the latest application list
cd /tmp
if [ -e "perfectbuntu6.current" ]; then
  gksudo --message "$GKMSG" "rm perfectbuntu6.current"
fi
if [ -e "perfectbuntu6.current.php?hostname=`hostname`\&name=`whoami`\&bit=$BIT\&ver=$VER\&release=`awk -F '=' '/DISTRIB_CODENAME/ {print $2}' /etc/lsb-release`" ]; then
  gksudo --message "$GKMSG" "rm perfectbuntu6.current.php?hostname=`hostname`\&name=`whoami`\&bit=$BIT\&ver=$VER\&release=`awk -F '=' '/DISTRIB_CODENAME/ {print $2}' /etc/lsb-release`"
fi
wget http://perfectbuntu.category5.tv/files/perfectbuntu6.current.php?hostname=`hostname`\&name=`whoami`\&bit=$BIT\&ver=$VER\&release=`awk -F '=' '/DISTRIB_CODENAME/ {print $2}' /etc/lsb-release` 2>&1 | sed -u 's/.* \([0-9]%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Downloading at \2\/s, ETA \3/' | zenity --progress --title="Downloading" --auto-close
mv perfectbuntu6.current.php?hostname=`hostname`\&name=`whoami`\&bit=$BIT\&ver=$VER\&release=`awk -F '=' '/DISTRIB_CODENAME/ {print $2}' /etc/lsb-release` perfectbuntu6.current
chmod +x perfectbuntu6.current

if [[ ! -e /tmp/perfectbuntu6.current ]] ; then
  echo "100";
  echo "# Error connecting to server.  Please try again later.";
  # add exit
fi

# Run the current list of installers
./perfectbuntu6.current "$LOGFILE" "$VER" "$ALLOW"

) |
zenity --width=400 --height=100 --progress --title="Perfectbuntu $VER - Progress" --text="Loading Perfectbuntu..." --percentage=0 --auto-kill

if [ "$?" = -1 ] ; then
zenity --error \
--text="Perfectbuntu aborted."
fi


